Skip to content

匹配绑定窗口图片3 - MatchWindowsFromPath

函数简介

匹配符合模板图片的坐标,模板通过文件路径指定,返回相对绑定窗口坐标,默认返回最优结果。

接口名称

MatchWindowsFromPath

DLL调用

long MatchWindowsFromPath(long ola, int x1, int y1, int x2, int y2, string templ, double matchVal, int type, double angle, double scale);

参数说明

参数名类型说明
ola长整数型OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。
x1整数型查找区域的左上角X坐标。
y1整数型查找区域的左上角Y坐标。
x2整数型查找区域的右下角X坐标。
y2整数型查找区域的右下角Y坐标。
templ字符串模板图片的路径,可用 | 分隔多图,如 "test.bmp|test2.bmp|test3.bmp"默认任一模板不存在或加载失败时整次返回空;可通过 SetConfigByKey 设置 MultiTemplateAbortOnInvalidImage=false 跳过无效模板继续匹配。
matchVal双精度浮点数相似度,如0.85,最大为1。
type整数型匹配类型:1-灰度匹配(速度快);2-彩色匹配;3-透明匹配;4-透明彩色权重匹配;5-普通彩色匹配。
angle双精度浮点数旋转角度,每次匹配后旋转指定角度继续匹配,角度越小匹配次数越多时间越长,0为不旋转速度最快。
scale双精度浮点数窗口缩放比例,默认为1,可通过GetScaleFromWindows接口获取。

示例

SDK 调用

cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
// 0,0,0,0 表示绑定窗口整个客户区
MatchData result = ola.MatchWindowsFromPath(0, 0, 0, 0, "img/template.bmp", 0.9, 1, 0, 1);
if (result.MatchState) {
    int x = result.X;
    int y = result.Y;
}
csharp
using OLAPlug;

var ola = new OLAPlugServer();
// 0,0,0,0 表示绑定窗口整个客户区
var result = ola.MatchWindowsFromPath(0, 0, 0, 0, "img/template.bmp", 0.9, 1, 0, 1);
if (result.MatchState)
{
    int x = result.X;
    int y = result.Y;
}
python
from OLAPlugServer import OLAPlugServer

ola = OLAPlugServer()
# 0,0,0,0 表示绑定窗口整个客户区
result = ola.MatchWindowsFromPath(0, 0, 0, 0, "img/template.bmp", 0.9, 1, 0, 1)
if result.get("MatchState"):
    x = result["X"]
    y = result["Y"]
java
import com.olaplug.OLAPlugServer;
import com.olaplug.model.MatchResult;
import java.util.List;

OLAPlugServer ola = new OLAPlugServer();
MatchResult result = ola.MatchWindowsFromPath(0, 0, 0, 0, "img/template.bmp", 0.9, 1, 0, 1);
if (result != null && result.MatchState) {
    int x = result.X;
    int y = result.Y;
}
go
import "github.com/ola/olaplug/olaplug"

ola, _ := olaplug.NewOLAPlugServer("OLAPlug_x64.dll")
defer ola.ReleaseObj()
// 0,0,0,0 表示绑定窗口整个客户区
MatchData result = ola.MatchWindowsFromPath(0, 0, 0, 0, "img/template.bmp", 0.9, 1, 0, 1)
if result.MatchState {
    x := result.X
    y := result.Y
}
rust
use olaplug::OLAPlugServer;

let ola = OLAPlugServer::new("OLAPlug_x64.dll").unwrap();
// 0,0,0,0 表示绑定窗口整个客户区
MatchData result = ola.match_windows_from_path(0, 0, 0, 0, "img/template.bmp", 0.9, 1, 0, 1)
if result.MatchState {
    let x = result.X;
    let y = result.Y;
}
cpp
var ola = com("OlaPlug.OlaSoft")
var result = ola.MatchWindowsFromPath(0, 0, 0, 0, "img/template.bmp", 0.9, 1, 0, 1)
if(result.MatchState) {
    var x = result.X
}
vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
Set result = ola.MatchWindowsFromPath(0, 0, 0, 0, "img/template.bmp", 0.9, 1, 0, 1)
If result.MatchState Then
text
.局部变量 ola, OLAPlug
.局部变量 result, MatchData
.局部变量 rawstring, 文本型
ola.创建 ()
result = ola.MatchWindowsFromPath (0, 0, 0, 0, "img/template.bmp", 0.9, 1, 0, 1, rawstring)
' rawstring 为原始 JSON 文本(可省略该参数)
.如果真 (result.MatchState)
aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var result = ola.MatchWindowsFromPath(0, 0, 0, 0, "img/template.bmp", 0.9, 1, 0, 1);
if(result.MatchState){
    var x = result.X;
}
text
变量 ola <类型 = OLAPlugServer>
变量 result <类型 = MatchData>
ola = 新建 OLAPlugServer
result = ola.MatchWindowsFromPath(0, 0, 0, 0, "img/template.bmp", 0.9, 1, 0, 1)
如果真 (result.MatchState)
cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
// 0,0,0,0 表示绑定窗口整个客户区
MatchData result = ola.MatchWindowsFromPath(0, 0, 0, 0, "img/template.bmp", 0.9, 1, 0, 1);
if (result.MatchState) {
    int32_t x = result.X;
    int32_t y = result.Y;
}

原生 DLL 调用

cpp
long instance = CreateCOLAPlugInterFace();
long ptr = MatchWindowsFromPath(instance, 0, 0, 0, 0, "img/template.bmp", 0.9, 1, 0, 1);
if (ptr != 0) {
    int matchState = 0, x = 0, y = 0, width = 0, height = 0, index = 0;
    double matchVal = 0, angle = 0;
    char json[4096] = {0};
    GetStringFromPtr(ptr, json, sizeof(json));
    ParseMatchImageJson(instance, json, &matchState, &x, &y, &width, &height, &matchVal, &angle, &index);
    FreeStringPtr(instance, ptr);
}
csharp
using System.Runtime.InteropServices;
using System.Text;

[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringFromPtr(long ptr, StringBuilder lpString, int size);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int FreeStringPtr(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringSize(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long MatchWindowsFromPath(long ola, int x1, int y1, int x2, int y2, string templ, double matchVal, int type, double angle, double scale);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int ParseMatchImageJson(string str, out int matchState, out int x, out int y, out int width, out int height, out double matchVal, out double angle, out int index);

long instance = CreateCOLAPlugInterFace();
long ptr = MatchWindowsFromPath(instance, 0, 0, 0, 0, "img/template.bmp", 0.9, 1, 0, 1);
if (ptr != 0) {
    StringBuilder sb = new StringBuilder(GetStringSize(ptr) + 1);
    GetStringFromPtr(ptr, sb, sb.Capacity);
    FreeStringPtr(ptr);
    ParseMatchImageJson(sb.ToString(), out int matchState, out int x, out int y, out int w, out int h, out double mv, out double ang, out int idx);
}
python
from ctypes import CDLL, c_int, c_int64, create_string_buffer

ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
ptr = ola.MatchWindowsFromPath(instance, ...)
# 原生返回 JSON 字符串指针,可用 ParseMatchImageJson 解析

返回值

调用方式返回值说明
SDKMatchData(C#/Java 为 MatchResult;Python 为 dict结构化匹配结果;MatchState 为真表示匹配成功
SDK(易语言/火山)MatchData内部已通过 GetStringFromPtr + OL_获取MatchData 解析
原生 DLL非 0成功,返回 JSON 字符串指针,需调用 FreeStringPtr 释放
原生 DLL0失败

返回数据格式:

json
{
    "MatchVal": 0.85,
    "MatchState": true,
    "Index": 0,
    "Angle": 45.0,
    "X": 100,
    "Y": 200,
    "Width": 100,
    "Height": 100
}
字段名类型说明
MatchVal浮点数匹配相似度。
MatchState布尔是否匹配成功。
Index整数结果索引(从 0 开始)。
Angle浮点数匹配角度。
X整数X 坐标。
Y整数Y 坐标。
Width整数宽度。
Height整数高度。

相关配置

配置项类型默认说明
MultiTemplateAbortOnInvalidImagebooltrue多模板路径 | 分隔时,某一图不存在或加载失败:true默认)整次返回空;false 跳过无效模板继续匹配(全局配置,见 SetConfigByKey)。

注意事项

项目说明
搜索范围x1、y1、x2、y2都传0时,将搜索整个窗口客户区。
坐标系返回的坐标是相对于绑定窗口客户区的坐标。